package test.patches;

import basemod.ReflectionHacks;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.evacipated.cardcrawl.modthespire.patcher.PatchingException;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import javassist.CannotCompileException;
import javassist.CtBehavior;
import javassist.expr.ExprEditor;
import javassist.expr.MethodCall;
import test.character.Captain;
import test.monitors.SeaMonitor;

import java.util.ArrayList;

public class CoordinateRenderPatch {

    @SpirePatch(
            clz = AbstractPlayer.class,
            method = "renderHand",
            paramtypez = {
                    SpriteBatch.class,
            }
    )
    public static class CustomCoordinatePatch {
        @SpireInsertPatch(
                locator = Locator.class
        )
        public static void Insert(AbstractPlayer __instance, SpriteBatch sb) {
            renderCoordinate(__instance, sb);
        }

        public static void renderCoordinate(AbstractPlayer p,SpriteBatch sb) {

            if(AbstractDungeon.player instanceof Captain){
                for(int i=0;i<10;i++){
                    SeaMonitor.Sea sea=SeaMonitor.seaList.get(i);
                    if(sea.card!=null){
                        AbstractCard card=sea.card;
                        float width = AbstractCard.IMG_WIDTH * card.drawScale / 2.0F;
                        float height = AbstractCard.IMG_HEIGHT * card.drawScale / 2.0F;
                        float topOfCard = card.current_y + height;
                        float textSpacing = 50.0F * Settings.scale;
                        float textY = topOfCard + textSpacing;
                        float sin = (float)Math.sin((double)(card.angle / 180.0F) * Math.PI);
                        float xOffset = sin * width;
                        FontHelper.renderFontCentered(sb, FontHelper.buttonLabelFont, Integer.toString(SeaMonitor.getSeaByCard(card).coordinate), card.current_x - xOffset, textY, Settings.CREAM_COLOR);
                    }else{
                        float width = AbstractCard.IMG_WIDTH * 0.75f / 2.0F;
                        float height = AbstractCard.IMG_HEIGHT * 0.75f / 2.0F;
                        float topOfCard = SeaMonitor.BLANK_SEA_POSITIONS_Y + height;
                        float textSpacing = 50.0F * Settings.scale;
                        float textY = topOfCard + textSpacing;
                        FontHelper.renderFontCentered(sb, FontHelper.buttonLabelFont, Integer.toString(sea.coordinate), SeaMonitor.BLANK_SEA_POSITIONS_X.get(i), textY, Settings.CREAM_COLOR);
                    }
                }
            }else{
                ReflectionHacks.privateMethod(AbstractPlayer.class,"renderCardHotKeyText",SpriteBatch.class)
                        .invoke(p,sb);
            }
        }

        private static class Locator extends SpireInsertLocator {
            public int[] Locate(CtBehavior ctMethodToPatch) throws CannotCompileException, PatchingException {
                Matcher finalMatcher = new Matcher.FieldAccessMatcher(AbstractPlayer.class, "inspectMode");

                int[] lines = LineFinder.findInOrder(ctMethodToPatch, new ArrayList<Matcher>(), finalMatcher);
                return new int[]{lines[0]};
            }
        }
    }


    @SpirePatch(
            clz = AbstractPlayer.class,
            method = "renderHand",
            paramtypez = {
                    SpriteBatch.class,
            }
    )
    public static class RemoveOriginCardHotKeyMethodPatch {
        @SpireInstrumentPatch
        public static ExprEditor Instrument() {
            return new ExprEditor() {
                @Override
                public void edit(MethodCall m) throws CannotCompileException {
                    if (m.getClassName().equals(AbstractPlayer.class.getName())
                            && m.getMethodName().equals("renderCardHotKeyText")) {
                        m.replace("{ "
                                + RemoveOriginCardHotKeyMethodPatch.class.getName()
                                + ".renderCardHotKeyText($0,sb); }");
                    }
                }
            };
        }

        public static void renderCardHotKeyText(AbstractPlayer p,SpriteBatch sb) {

        }
    }
}
